package controle;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import modelo.*;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;

public class XML {

   XStream xstream = new XStream(new StaxDriver());
	
	 private String readFile( String file) throws IOException {

		    BufferedReader reader = new BufferedReader( new FileReader (file));
		    String line  = null;
		    StringBuilder stringBuilder = new StringBuilder();
		    String ls = System.getProperty("line.separator");
		    while( ( line = reader.readLine() ) != null) {
			        stringBuilder.append( line );
			        stringBuilder.append( ls );
		    }
		    
		    return stringBuilder.toString();
	}
   
     public String criaXML(Object a) {
    	 
        String s = xstream.toXML(a);
        
        return s;
     
     }
     
       public Personagem leXMLPersonagem(String save) {
       
        Personagem m = new Personagem();
		
        try {
           m = (Personagem)xstream.fromXML(this.readFile(save));
        } catch (IOException e) {
     
           e.printStackTrace();
        }
        
        return m;
       
     }

       
       public Inimigos leXMLInimigos() {
    	   
    	   Inimigos um = new Inimigos();
   		
           xstream.alias("Inimigos", Inimigos.class);
           xstream.alias("Inimigo", Inimigo.class);
           
           try {
    		um = ((Inimigos)xstream.fromXML(this.readFile("inimigos.xml")));
    	} catch (IOException e) {

    		e.printStackTrace();
    	}
           return um;
           
    	}

       public Lugares leXMLLugares() {
    	   
           Lugares um = new Lugares();
    		
           xstream.alias("Lugares", Lugares.class);
           xstream.alias("Lugar", Lugar.class);
           xstream.alias("String", String.class);
           
           try {
			um = ((Lugares)xstream.fromXML(this.readFile("lugares.xml")));
		} catch (IOException e) {
		
			e.printStackTrace();
		}
           return um;
           
    	}
}
